package com.hzdp.coc;

import java.util.ArrayList;
import java.util.List;

public class NotifyDemo {

	private int MAX = 2;
	private List<Object> buf = new ArrayList<Object>();

	public synchronized void put(Object o) {
		while (MAX == buf.size()) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		buf.add(o);
	}

	public synchronized void noAl() {
		notifyAll();
	}
	public synchronized Object get() {
		while (buf.size() == 0) {
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return buf.remove(buf.size() - 1);
	}

	static class Producer implements Runnable {

		private NotifyDemo demo;
		public Producer(NotifyDemo demo) {
			this.demo = demo;
		}
		@Override
		public void run() {
			demo.put(1);
		}
		
	}
	
	static class Notifier implements Runnable {

		private NotifyDemo demo;
		public Notifier(NotifyDemo demo) {
			this.demo = demo;
		}
		@Override
		public void run() {
			demo.noAl();
		}
		
	}
	
	
	public static void main(String[] args) throws InterruptedException {
		final NotifyDemo demo = new NotifyDemo();
		new Thread(new Producer(demo)).start();
		new Thread(new Producer(demo)).start();
		new Thread(new Notifier(demo)).start();
		Thread.sleep(10000);
	}
}
